package com.gtp.mq.demo.spring.consumer;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.gtp.mq.demo.spring.utils.DateUtils;
import com.rabbitmq.client.Channel;

/**
 * 消费模式1,监听指定队列
 *
 * @author gaotingping@janlc.com
 * @date 2019年8月29日
 */
@Component
public class MyConsumer {

	/**
	 * 注解模式
	 * 可以在这个注解上:声明队列、交换机、绑定关系
	 * 但是不建议这样弄,统一的配置管理类更好
	 * 
	 * #默认是自动ack,本方法内不抛出异常时，就自动ack
	 */
	@RabbitListener(queues="test_queue1")
	public void test1(String msg,Channel channel, Message message){
		
		System.out.println(DateUtils.nowDate()+" 收到="+msg);
		
		//throw new RuntimeException("抛出异常！");
		/*
		 * issue fix:
		 * 抛出异常后，消息会重试(但是注释死循环,重新尝试的消费，应该有重试间隔，超过指定次数丢弃等特性)
		 * 
		 * issue fix:
		 * 可以指定重试间隔,最大重试次数等
		 * https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
		 */
		//throw new RuntimeException("抛出异常!!!");
		//issue fix:这里宕机了，消息还没有确认，重启后会继续消费
		//auto模式下，检测异常等来决定消息是否ack
	}
	
	//Fanout
	@RabbitListener(queues="Fanout_queue1")
	public void testFanout1(String msg){
		System.out.println("Fanout1 msg="+msg);
	}
	
	@RabbitListener(queues="Fanout_queue2")
	public void testFanout2(String msg){
		System.out.println("Fanout2 msg="+msg);
	}
	
	//direct
	@RabbitListener(queues="Direct_queue1")
	public void testDirect1(String msg){
		System.out.println("Direct1 msg="+msg);
	}
	
	@RabbitListener(queues="Direct_queue2")
	public void testDirect2(String msg){
		System.out.println("Direct2 msg="+msg);
	}
	
	//topic
	@RabbitListener(queues="Topic_queue1")
	public void testTopic1(String msg){
		System.out.println("Topic1 msg="+msg);
	}
	
//	@RabbitListener(queues="Topic_queue2")
//	public void testTopic2(String msg){
//		System.out.println("Topic2 msg="+msg);
//	}
}
